using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.IService.Sys;

namespace net_work.Controllers.Sys;

[Route("api/[controller]")]
[ApiController]
public class CollegeController(ILogger<CollegeController> logger, ICollegeService collegeService) : ControllerBase
{
    /// <summary>
    /// 获取学院列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListColleges([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取学院列表");
        var result = await collegeService.GetCollegesPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取学院信息
    /// </summary>
    /// <param name="collegeId">学院ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetCollegeById([FromQuery] long collegeId)
    {
        logger.LogInformation("获取学院信息");
        return await collegeService.SelectByIdAsync(collegeId) is { } college
            ? AjaxResult.Success(college)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增学院
    /// </summary>
    /// <param name="collegeAddRequest">新增学院实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> AddCollege([FromBody] CollegeAddRequest collegeAddRequest)
    {
        logger.LogInformation("新增学院");
        var collegeId = await collegeService.InsertCollege(collegeAddRequest);
        return collegeId > 0
            ? AjaxResult.Success(new { collegeId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改学院
    /// </summary>
    /// <param name="collegeUpdateRequest">修改学院实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> UpdateCollege([FromBody] CollegeUpdateRequest collegeUpdateRequest)
    {
        logger.LogInformation("修改学院");
        return await collegeService.UpdateCollege(collegeUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除学院
    /// </summary>
    /// <param name="ids">学院ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> DeleteCollege([FromBody] List<long> ids)
    {
        logger.LogInformation("删除学院");
        return await collegeService.DeleteCollegeByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}